package com.android.pdd.baseapp.dialog;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.android.pdd.baseapp.R;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ScreenUtils;

public class FloatView {
    private View floatView;
    WindowManager manager;
    WindowManager.LayoutParams params;

    private Activity activity;
    private int iconResId;

    public FloatView(Activity activity, int iconResId) {
        this.activity = activity;
        this.iconResId = iconResId;
        manager = (WindowManager) activity.getApplication().getSystemService(Context.WINDOW_SERVICE);
        params = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //26及以上必须使用TYPE_APPLICATION_OVERLAY   @deprecated TYPE_PHONE
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            params.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        //设置flags
        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.START | Gravity.TOP;
        //背景设置成透明
        params.format = PixelFormat.TRANSPARENT;
        screenHeight = ScreenUtils.getScreenHeight();
        screenWidth = ScreenUtils.getScreenWidth();
        params.x = screenWidth;
        params.y = screenHeight / 2;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addCategory(Intent.CATEGORY_HOME);
            activity.startActivity(intent);
            return true;
        }
        return false;
    }

    public void onWindowFocusChanged(boolean hasFocus) {
        if (hasFocus) {
            if (floatView != null) {
                manager.removeView(floatView);
                floatView = null;

            }
        }
    }

    public void onStop() {
        if (floatView == null && !activity.isFinishing()) {
            addCartView();
        }
    }

    private int mLastX, mLastY;

    private int screenHeight;

    private int floatWidth;

    private int lastParamsX, lastParamsY;

    long clickTime;
    private boolean isMoved = false;
    private int right;
    private int screenWidth;

    private void addCartView() {
        floatView = LayoutInflater.from(activity).inflate(R.layout.view_float_menu, null);
        ImageView imageView = floatView.findViewById(R.id.iv_float_img);
        imageView.setImageResource(iconResId);
        floatView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                Log.e("FloatView", "ACTION_MOVE :" + (int) event.getRawX() + "," + (int) event.getRawY());
                if (floatWidth == 0) {
                    floatWidth = floatView.getWidth();
                }
//                if (floatView.isSelected()) {
//                    return false;
//                }
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mLastX = (int) event.getRawX();
                        mLastY = (int) event.getRawY();
                        lastParamsX = params.x;
                        lastParamsY = params.y;
                        clickTime = System.currentTimeMillis();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.e("FloatView", "ACTION_MOVE :" + (int) event.getRawX() + "," + (int) event.getRawY());
                        isMoved = true;
                        params.x = (int) (event.getRawX() - mLastX + lastParamsX);
                        params.y = (int) (event.getRawY() - mLastY + lastParamsY);
                        manager.updateViewLayout(floatView, params);
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.e("FloatView", "params.x:" + params.x + ",params.y:" + params.y);
                        right = floatWidth + params.x;
                        if (isMoved) {
                            if (params.x < (screenWidth - floatWidth) / 2) {
                                if (params.x > 0) { //left=0时已经是贴左边了
                                    Log.e("FloatView", "贴左边 :" + params.x + ",0," + params.y);
                                    startScroll(params.x, 0, params.y);
                                }
                            } else {
                                if (right < screenWidth) {//left=screenWidth时已经是贴右边了
                                    Log.e("FloatView", "贴右边:" + (right - floatWidth) + ",screenWidth - floatWidth," + params.y);
                                    startScroll(right - floatWidth, screenWidth - floatWidth, params.y);
                                }
                            }
                            isMoved = false;
                        } else {
                            if (System.currentTimeMillis() - clickTime < 1200) {
//                                floatView.setSelected(true);
                                AppUtils.launchApp(activity.getPackageName());
                            }
                        }
                        break;
                }
                return true;
            }
        });
        manager.addView(floatView, params);

    }

    public void startScroll(final int start, int end, final int top) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end).setDuration(300);
        Log.i("CartView", "startScroll---start:" + start + ",end:" + end);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                params.x = (int) animation.getAnimatedValue();
                params.y = top;
                manager.updateViewLayout(floatView, params);
            }
        });
        valueAnimator.start();
    }
}
